//
//  changepasswordController.m
//  pacc
//
//  Created by 测试 on 2018/5/23.
//  Copyright © 2018年 测试. All rights reserved.
//

#import "changepasswordController.h"

@interface changepasswordController ()<RHURLRequestDelegate,UITextFieldDelegate>
@property(strong,nonatomic)NetWorking *request;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTF;
@property (weak, nonatomic) IBOutlet UIButton *huoquyanzhengmaBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;

@property (weak, nonatomic) IBOutlet UITextField *yanzhengmaTF;
@property (weak, nonatomic) IBOutlet UITextField *newnewPassword;
@property (weak, nonatomic) IBOutlet UITextField *nPasswordTF;
- (IBAction)sureClick:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;

@end

@implementation changepasswordController
- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)mingmiwenClick:(UIButton *)sender {
    _nPasswordTF.secureTextEntry = !_nPasswordTF.secureTextEntry;
    if (_nPasswordTF.secureTextEntry) {
        [sender setImage:[UIImage imageNamed:@"eye_off"] forState:UIControlStateNormal];
    }else          [sender setImage:[UIImage imageNamed:@"eye_on"] forState:UIControlStateNormal];

}
- (IBAction)mingmiwenClick2:(UIButton *)sender {
    _newnewPassword.secureTextEntry = !_newnewPassword.secureTextEntry;
    if (_newnewPassword.secureTextEntry) {
        [sender setImage:[UIImage imageNamed:@"eye_off"] forState:UIControlStateNormal];
    }else          [sender setImage:[UIImage imageNamed:@"eye_on"] forState:UIControlStateNormal];

}
- (IBAction)huoquyanzhengmaClick:(id)sender {
        NSString * PhoneStr = _phoneNumberTF.text;
        if (PhoneStr.length != 11) {
            [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
              [SVProgressHUD setBackgroundColor:RGB(85, 85, 85)];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]];;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            return;
        }
        [NetWorking startPOSTRequestWithUrl:[NSString stringWithFormat:@"usersendSms"] parameters:@{@"phone":PhoneStr} logMes:@"获取验证码" show:NO success:^(NSDictionary *data) {
            [self openCountdown];
            
        } failure:^(NSError *error) {
            
        }];
    }
    // 开启倒计时效果
    -(void)openCountdown{
        
        __block NSInteger time = 59; //倒计时时间
        
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
        
        dispatch_source_set_event_handler(_timer, ^{
            
            if(time <= 0){ //倒计时结束，关闭
                
                dispatch_source_cancel(_timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    //设置按钮的样式
                    [self.huoquyanzhengmaBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                    [self.huoquyanzhengmaBtn setTitleColor:RGB(60, 129, 240) forState:UIControlStateNormal];
                    self.huoquyanzhengmaBtn.userInteractionEnabled = YES;
                });
                
            }else{
                
                int seconds = time % 60;
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    //设置按钮显示读秒效果
                    [self.huoquyanzhengmaBtn setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];
                    [self.huoquyanzhengmaBtn setTitleColor:RGB(136, 136, 136) forState:UIControlStateNormal];
                    self.huoquyanzhengmaBtn.userInteractionEnabled = NO;
                });
                time--;
            }
        });
        dispatch_resume(_timer);
    }
- (NetWorking *)request {
    
    if (!_request) {
        _request = [[NetWorking alloc] init];
        _request.delegate = self;
    }
    return _request;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _backBtn.hidden = _isHidden;
    if (_isFromWO) {
        _topConstraint.constant =0;
    }
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)sureClick:(UIButton *)sender {
    if (!_nPasswordTF.text.length||!_phoneNumberTF.text.length||!_yanzhengmaTF.text.length||!_newnewPassword.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的信息"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        return;
    }
    if (!_phoneNumberTF.text.length) {
        return;
    }
    if (!_yanzhengmaTF.text.length) {
        return;
    }
    if (!_newnewPassword.text.length) {
        return;
    }
    if (_nPasswordTF.text != _newnewPassword.text) {
        [SVProgressHUD showErrorWithStatus:@"两次密码输入不一致"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        return;
    }
    NSString *oldPhoneNum = _phoneNumberTF.text;
    NSString *password = _newnewPassword.text;
    NSString *code = _yanzhengmaTF.text;
    [NetWorking startPOSTRequestWithUrl:[NSString stringWithFormat:@"userupdatePassword"] parameters:@{@"password":password,@"phone":oldPhoneNum,@"code":code} logMes:@"修改密码" show:NO success:^(NSDictionary *data) {
        NSString *status = [NSString stringWithFormat:@"%@",[data objectForKey:@"status"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([status isEqualToString:@"200"]) {
                NSString *msg = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
                [SVProgressHUD showWithStatus:msg];
                  [SVProgressHUD setBackgroundColor:RGB(85, 85, 85)];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]];;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                    [self dismissViewControllerAnimated:YES completion:nil];
                });
            }else{
                NSString *msg = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
                [SVProgressHUD showErrorWithStatus:msg];
                  [SVProgressHUD setBackgroundColor:RGB(85, 85, 85)];
                [SVProgressHUD setForegroundColor:[UIColor whiteColor]];;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
            }
             });
    } failure:^(NSError *error) {
        
    }];
}
@end
